package com.bishe.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.StrUtil;
import com.api.EcoBootException;
import com.api.Result;
import com.api.entitis.JobsInfo;
import com.api.entitis.query.JobsInfoQuery;
import com.api.service.IJobsInfoService;
import com.api.util.CompanyStpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("/job")
@Api(tags = "招聘信息控制器")
public class JobsInfoController {

    @Resource
    IJobsInfoService jobsInfoService;

    @ApiOperation("添加或修改招聘信息")
    @PostMapping("/addOrUpdate")
    @SaCheckLogin(type = CompanyStpUtil.TYPE)
    public Result addOrUpdate(@RequestBody JobsInfo jobsInfo){

        if (jobsInfo == null){
            throw new EcoBootException("信息不能为空");
        }

        if (StrUtil.isEmpty(jobsInfo.getEducation())||
            StrUtil.isEmpty(jobsInfo.getPost()) ||
                StrUtil.isEmpty(jobsInfo.getPay())||
                StrUtil.isEmpty(jobsInfo.getCompanyName())||
                StrUtil.isEmpty(jobsInfo.getWorkPlace())||
                StrUtil.isEmpty(jobsInfo.getWorkYear())||
                StrUtil.isEmpty(jobsInfo.getEducation())||
                StrUtil.isEmpty(jobsInfo.getPositionStatement())||
                StrUtil.isEmpty(jobsInfo.getJobRequirements())||
                StrUtil.isEmpty(jobsInfo.getSocialBenefits())
        ){
            throw new EcoBootException("招聘信息不能为空");
        }

        jobsInfoService.addOrUpdate(jobsInfo);
        return Result.ok("添加成功");
    }

    @ApiOperation("删除信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        jobsInfoService.delete(id);
        return Result.ok();
    }

    @ApiOperation("根据id查询信息")
    @GetMapping("/select/{id}")
    public Result selectById(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        //根据id查询
        JobsInfo jobsInfo = jobsInfoService.getById(id);
        if (jobsInfo != null){
            return Result.ok(jobsInfo);
        }
        return Result.error("信息不存在");
    }

    @ApiOperation("分页查询")
    @GetMapping("/toPage")
    public Result toPage(JobsInfoQuery query){

        //分页处理
        IPage<JobsInfo> iPage = jobsInfoService.toPage(query);
        return Result.ok(iPage);
    }
}
